<%@ page import="service.UseOrderService" %>
<%@ page import="beans.UseOrder" %>
<%@ page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    // 检查session是否存在
    if(session.getAttribute("username") == null) {
        response.sendRedirect(request.getContextPath() + "/login.jsp");
        return;
    }
    String username = (String)session.getAttribute("username");
    String role = (String)session.getAttribute("role");
    Object user = session.getAttribute("user");
    int driverId = Integer.parseInt(username); // 获取当前登录司机的ID
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>全部订单 - 公司用车管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        /* 全局样式 */
        body {
            margin: 0;
            padding: 0;
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f6f6f6;
            color: #333;
        }

        /* 头部样式 */
        .header {
            background: #183a8a;
            color: #fff;
            font-size: 2rem;
            font-weight: bold;
            padding: 10px 0 10px 10px;
            letter-spacing: 2px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .header-right {
            display: flex;
            align-items: center;
            margin-right: 30px;
            font-size: 1rem;
        }
        .user-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #fff;
            color: #183a8a;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2rem;
            margin-right: 10px;
        }
        .user-info {
            margin-right: 20px;
            color: #fff;
        }
        .bell-icon {
            font-size: 1.3rem;
            margin-right: 20px;
            color: #fff;
        }
        .logout {
            color: #fff;
            text-decoration: none;
            margin-left: 10px;
            font-size: 1rem;
            padding: 8px 15px;
            border-radius: 4px;
            background: rgba(255,255,255,0.1);
            transition: background 0.3s ease;
        }
        .logout:hover {
            background: rgba(255,255,255,0.2);
        }

        /* 主体容器 */
        .container {
            display: flex;
            height: calc(100vh - 56px);
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 220px;
            background: #183a8a;
            display: flex;
            flex-direction: column;
            padding-top: 10px;
            box-shadow: 2px 0 8px rgba(24,58,138,0.05);
        }
        .menu-item {
            color: #fff;
            padding: 18px 0 18px 32px;
            font-size: 1.15rem;
            cursor: pointer;
            border-left: 5px solid transparent;
            transition: background 0.2s, border 0.2s;
            text-decoration: none;
            display: flex;
            align-items: center;
        }
        .menu-item i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }
        .menu-item.active, .menu-item:hover {
            background: #2450a7;
            border-left: 5px solid #fff;
            font-weight: bold;
            box-shadow: 0 4px 8px rgba(24,58,138,0.10);
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            background: #fff;
            border: 2px solid #3ba0e9;
            border-left: none;
            padding: 20px;
            overflow-y: auto;
        }
        .search-container {
            margin-bottom: 20px;
        }
        .search-input {
            width: 300px;
            padding: 8px 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
        }
        .search-btn {
            padding: 8px 15px;
            background: #1da1e7;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1rem;
            margin-left: 10px;
        }
        .search-btn:hover {
            background: #2176b8;
        }
        .application-list {
            margin-top: 20px;
        }
        .application-item {
            padding: 15px;
            margin-bottom: 10px;
            background: #f8f9fa;
            border-radius: 4px;
            border-left: 4px solid #1da1e7;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .application-info {
            flex: 1;
        }
        .view-btn {
            padding: 8px 15px;
            background: #1da1e7;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.9rem;
            margin-left: 15px;
        }
        .view-btn:hover {
            background: #2176b8;
        }
        .no-results {
            text-align: center;
            padding: 20px;
            color: #666;
        }
        .status-badge {
            display: inline-block;
            padding: 3px 8px;
            color: white;
            border-radius: 3px;
            font-size: 0.8rem;
            margin-left: 10px;
        }
        .status-pending {
            background: #ff9800;
        }
        .status-approved {
            background: #4caf50;
        }
        .status-rejected {
            background: #f44336;
        }
        .status-completed {
            background: #2196f3;
        }
        /* 添加弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 300px;
            border-radius: 5px;
            text-align: center;
        }
        .modal-buttons {
            margin-top: 20px;
        }
        .modal-btn {
            padding: 8px 20px;
            margin: 0 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .confirm-btn {
            background: #4caf50;
            color: white;
        }
        .cancel-btn {
            background: #f44336;
            color: white;
        }
    </style>
</head>
<body>
<div class="header">
    <div>欢迎登录公司用车管理系统（司机）</div>
    <div class="header-right">
        <div class="user-icon">👤</div>
        <div class="user-info">工号：<%=username%></div>
        <div class="bell-icon">🔔</div>
        <a href="<%=request.getContextPath()%>/login.jsp" class="logout">退出登录</a>
    </div>
</div>

<div class="container">
    <div class="sidebar">
        <a class="menu-item" href="DriversServlet?action=1234">
            <i class="fas fa-home"></i> 系统首页
        </a>
        <a class="menu-item" href="DriversServlet?action=a">
            <i class="fas fa-user"></i> 个人中心
        </a>
        <%--    <a class="menu-item" href="DriversServlet?action=b">--%>
        <%--      <i class="fas fa-tasks"></i> 我的待办--%>
        <%--    </a>--%>
        <a class="menu-item active" href="DriversServlet?action=c">
            <i class="fas fa-list"></i> 全部订单
        </a>
        <a class="menu-item" href="DriversServlet?action=d">
            <i class="fas fa-tools"></i> 维修单提交
        </a>
        <a class="menu-item" href="DriversServlet?action=e">
            <i class="fas fa-money-bill-wave"></i> 用车费用提交
        </a>
    </div>

    <div class="main-content">
        <div class="search-container">
            <form method="get" action="">
                <input type="text" name="keyword" class="search-input"
                       placeholder="搜索用车申请"
                       value="${param.keyword}">
                <button type="submit" class="search-btn">搜索</button>
            </form>
        </div>

        <div class="application-list">
            <%
                String keyword = request.getParameter("keyword");
                UseOrderService orderService = new UseOrderService();
                List<UseOrder> orders = null;
                boolean hasError = false;

                try {
                    if (keyword != null && !keyword.trim().isEmpty()) {
                        orders = orderService.searchUseOrders(keyword);
                    } else {
                        orders = orderService.getAllUseOrders();
                    }
                    
                    // 过滤出当前司机的订单
                    if (orders != null) {
                        orders.removeIf(order -> order.getDriverId() != driverId);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    hasError = true;
                }
            %>

            <% if (hasError) { %>
            <div class="no-results">系统错误，无法获取订单数据</div>
            <% } else if (orders == null || orders.isEmpty()) { %>
            <div class="no-results">您还没有任何订单记录</div>
            <% } else {
                for (UseOrder order : orders) {
                    String applicantName = "员工" + order.getEmployeeId();
                    String destination = order.getUsageReason();
                    String formattedDate = new java.text.SimpleDateFormat("yyyy-MM-dd").format(order.getExpectedDepartureTime());
                    String statusClass = "";
                    switch(order.getReviewStatus()) {
                        case "待审核":
                            statusClass = "status-pending";
                            break;
                        case "已通过":
                            statusClass = "status-approved";
                            break;
                        case "已拒绝":
                            statusClass = "status-rejected";
                            break;
                        case "已完成":
                            statusClass = "status-completed";
                            break;
                    }
            %>
            <div class="application-item">
                <div class="application-info">
                    <div>用车申请<%= order.getOrderId() %>：<%= applicantName %> - <%= destination %> - <%= formattedDate %></div>
                    <span class="status-badge <%= statusClass %>"><%= order.getReviewStatus() %></span>
                </div>
                <button class="view-btn" onclick="handleViewOrder('<%= order.getOrderId() %>', '<%= order.getReviewStatus() %>')">操作</button>
            </div>
            <%   }
            } %>
        </div>
    </div>
</div>

<!-- 添加弹窗 -->
<div id="confirmModal" class="modal">
    <div class="modal-content">
        <h3>确认完成订单</h3>
        <p>是否已完成此订单？</p>
        <div class="modal-buttons">
            <button class="modal-btn confirm-btn" onclick="confirmComplete()">确认完成</button>
            <button class="modal-btn cancel-btn" onclick="closeModal()">取消</button>
        </div>
    </div>
</div>

<script>
    let currentOrderId = null;
    
    function handleViewOrder(orderId, status) {
        if (status === '已通过') {
            currentOrderId = orderId;
            document.getElementById('confirmModal').style.display = 'block';
        } else {
            window.location.href = '<%=request.getContextPath()%>/pirt/drivers/OrderDetail.jsp?orderId=' + orderId;
        }
    }
    
    function confirmComplete() {
        // 发送请求更新订单状态
        fetch('UseOrderServlet?action=updateStatus&orderId=' + currentOrderId + '&status=已完成', {
            method: 'POST'
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('订单状态已更新为已完成');
                window.location.reload();
            } else {
                alert('更新失败：' + data.message);
            }
        })
        .catch(error => {
            alert('系统错误，请稍后重试');
        });
        closeModal();
    }
    
    function closeModal() {
        document.getElementById('confirmModal').style.display = 'none';
        currentOrderId = null;
    }
    
    // 点击模态框外部关闭
    window.onclick = function(event) {
        if (event.target == document.getElementById('confirmModal')) {
            closeModal();
        }
    }
</script>
</body>
</html>